<script setup>
import { storeToRefs } from 'pinia';
const dayjs = useDayjs()

const props = defineProps({
	nkv: String,
	naryad_id: Number,
	specList: Object,
	get_nar_usl: Function,
	year: Number,
	month: Number,
	mpp2: Number,
	profile: Number,
})

const mppTech = ref(0);
const mppLit = ref(0);
const mppPol = ref(0);
const mppCadcam = ref(0);

const urlHome = useUrl();

async function getSpecByNar() {
	const response = await $fetch(`${urlHome.value}lab/get_spec_from_nar/`, {
		query: {
			nkv: props.nkv
		}
	})
	if (response.ok) {
		mppTech.value = response.tech
		mppLit.value = response.lit
		mppPol.value = response.pol
		mppCadcam.value = response.cadcam
	}
}

await getSpecByNar()

async function changeSpec() {
	let formData = new FormData();
	formData.append('tech', mppTech.value)
	formData.append('lit', mppLit.value)
	formData.append('pol', mppPol.value)
	formData.append('cadcam', mppCadcam.value)
	formData.append('naryad_id', props.naryad_id)

	const response = await $fetch(`${urlHome.value}lab/sdl_set_update/`, {
		method: 'POST',
		body: formData
	})
	if (response.ok) {
		await finishNaryad()
	}
}

async function finishNaryad() {
	const response = await $fetch(`${urlHome.value}lab/finish_naryad`, {
		query: {
			nkv: props.nkv,
			year: props.year,
			month: props.month,
			profile: props.profile,
			mpp: props.mpp2,
		}
	})
	if (response.ok) {
		props.get_nar_usl()
	}
}
</script>

<template>
	<div class="flex flex-nowrap items-center">
		<UButton label="Верно" size="xs" @click="changeSpec" />
		<USelect color="primary" v-model="mppTech" :options="specList.list_tech" option-attribute="name"
			value-attribute="mpp" />
		<USelect color="primary" v-model="mppLit" :options="specList.list_lit" option-attribute="name"
			value-attribute="mpp" />
		<USelect color="primary" v-model="mppPol" :options="specList.list_pol" option-attribute="name"
			value-attribute="mpp" />
		<USelect color="primary" v-model="mppCadcam" :options="specList.list_cadcam" option-attribute="name"
			value-attribute="mpp" />
	</div>
</template>
